// +----------------------------------------------------------------------
// | ADM Software [ 我宁可 为做过的事而后悔，也不想 因为没尝试而后悔。 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州安蝶梦电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.andiemeng.com>
// +----------------------------------------------------------------------
package com.gr100.biz.kit.bmap;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/*
 * 百度中文地址逆推 坐标方法
 */
public class GeoCoder {

	/*
	 * 传人中文地址、和城市 返回 fastjson 格式json 对象包含经纬度和可信度
	 */
	public static JSONObject Geocoding(String address,String city){
		String result="";
		HttpResponse response;
		JSONObject json=new JSONObject();
		try {
			address= URLEncoder.encode(address, "utf-8");
			city= URLEncoder.encode(city, "utf-8");
			String url="http://api.map.baidu.com/geocoder/v2/?ak=W7fSw2UYZWEZsT8sr0jH6OuF&output=json&address="+address+"&city="+city;
			HttpGet request = new HttpGet(url);
			response = HttpClients.createDefault().execute(request);
			if(response.getStatusLine().getStatusCode()==200){
				result = EntityUtils.toString(response.getEntity());
				json=JSONObject.parseObject(result);
			}else{
				json.put("status", -1);
				json.put("message","发生错误，百度服务器。");
			}
		} catch (ClientProtocolException e) {
			json.put("status", -1);
			json.put("message","发生错误。");
		} catch (IOException e) {
			json.put("status", -1);
			json.put("message","发生错误。");
		}
		return  json;
	}
	
}
